/**********************************************************************
  The MIT License (MIT)
  Copyright (c) 2012 sunriseyuen

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/

#define _DEF_CCY_CODE
#define _ZZDEBUG
#define _PROJ_DC

#region Using directives
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using Muki.Framework.Util;
using Muki.Framework.Business;
using Muki.Framework;
using Muki.UserDefine;
using Muki.Entity;
using Dot.StreamObject;

#endregion

namespace Muki.UserPanel
{
	public class UID02206BM:BLogic
	{
		public UID02206BM()
		{
			objDefault.UID_CODE    ="UID02206B";
		}

		public void Initialize()
		{
			objDefault.ZZLOGON_SSD = this.ZZLOGON_SSD;
			objDefault.Trans       = this.Trans;
			objDefault.ZZLOGON_ID  = this.ZZLOGON_ID;

			_Convert.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_Convert.Trans       = this.Trans;

			_DataPermission.ZZLOGON_SSD = this.ZZLOGON_SSD;
			_DataPermission.Trans       = this.Trans;
			_DataPermission.TABLE_NAME  = "AREDTLM";
			_DataPermission.ZZLOGON_ID  = this.ZZLOGON_ID;
			_DataPermission.DataAccessRight();

			_DocsPermission.ZZLOGON_SSD  = this.ZZLOGON_SSD;
			_DocsPermission.Trans        = this.Trans;

			_Setting.ZZLOGON_ID = this.ZZLOGON_ID;
			_Setting.Trans      = this.Trans;

		}

		public UID02206BE Verify(UID02206BE _Entity)
		{
    _Entity.SIGN_CODE=_Entity.SIGN_CODE.ToUpper();

    if (_Entity.ARE_CCY_AMT<=0)
    {
        throw new LogisticException("ARE_CCY_AMT","DataMiss","Validate");
    }

						_Entity.ARE_DT=_Entity.ARE_DT.Trim();

					if (Util.IsNullOrEmpty(_Entity.CCY_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.XCHG_RATE))
					{
						_Entity.Exceptions.Add(new LogisticException("XCHG_RATE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.ACC_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataMiss"));
					}

					string cUGET_XREF="{D,C}";
					if (cUGET_XREF.IndexOf(_Entity.SIGN_CODE)<0)
					{
						_Entity.Exceptions.Add(new LogisticException("SIGN_CODE","InvalidData",_Entity.SIGN_CODE));
					}

					if (Util.IsNullOrEmpty(_Entity.SIGN_CODE))
					{
						_Entity.Exceptions.Add(new LogisticException("SIGN_CODE","DataMiss"));
					}

					if (Util.IsNullOrEmpty(_Entity.ARE_CCY_AMT))
					{
						_Entity.Exceptions.Add(new LogisticException("ARE_CCY_AMT","DataMiss"));
					}

				// Relation CCYRTBM
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataNotFd",_Entity.CCY_CODE));
			}else{

					CCYRTBMEntity objVCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objVCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objVCCYRTBMEntity.CCY_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("CCY_CODE","DataNotFd",_Entity.CCY_CODE));
					}
			}
			}
//CCYRTBM

				// Relation ACCDTBM
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

			if (objEntityContainer.Count==0)
			{
				_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataNotFd",_Entity.ACC_CODE));
			}else{

					ACCDTBMEntity objVACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objVACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objVACCDTBMEntity.ACC_CODE;

					}else{
						_Entity.Exceptions.Add(new LogisticException("ACC_CODE","DataNotFd",_Entity.ACC_CODE));
					}
			}
			}
//ACCDTBM

    CriteriaRetrieve objValidateEntity=new CriteriaRetrieve(typeof(AREHDTMEntity));
    objCondition=objValidateEntity.NewCondition();
    objCondition.Equals("ARE_NO",_Entity.ARE_NO);

    EntityObject objValidateObject=this.Trans.RetrieveEntity(objValidateEntity);

    if(objValidateObject!=null)
    {
        AREHDTMEntity objAREHDTMEntity=(AREHDTMEntity)objValidateObject;

        ZZStatusCheck oZZStatusCheck=new ZZStatusCheck();
        oZZStatusCheck.Trans=this.Trans;
        if (!oZZStatusCheck.Validate("ODTP_CODE_DT",objAREHDTMEntity.ODTP_STATUS,_Entity.ATVT_CODE))
        {
            throw new LogisticException("ODTP_STATUS",oZZStatusCheck.Caption,objAREHDTMEntity.ODTP_STATUS);
        }
    }

				return _Entity;
		}

		public List<UID02206BE> Verify(List<UID02206BE> _Entitys)
		{
			List<UID02206BE> _New_Entitys=new List<UID02206BE>();

			foreach (UID02206BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Verify(_Entity));
			}
			return _New_Entitys;
		}

		public UID02206BE Default(UID02206BE _Entity)
		{

						_Entity.ARE_DT=objDefault.getDefault("ARE_DT",_Entity.ARE_DT);
						_Entity.SERIAL_NO=objDefault.getDefault("SERIAL_NO",_Entity.SERIAL_NO);
						_Entity.CCY_CODE=objDefault.getDefault("CCY_CODE",_Entity.CCY_CODE);
						_Entity.XCHG_RATE=objDefault.getDefault("XCHG_RATE",_Entity.XCHG_RATE);
						_Entity.ACC_CODE=objDefault.getDefault("ACC_CODE",_Entity.ACC_CODE);
						_Entity.ACC_NAME01=objDefault.getDefault("ACC_NAME01",_Entity.ACC_NAME01);
						_Entity.SIGN_CODE=objDefault.getDefault("SIGN_CODE",_Entity.SIGN_CODE);
						_Entity.ARE_CCY_AMT=objDefault.getDefault("ARE_CCY_AMT",_Entity.ARE_CCY_AMT);
						_Entity.ARE_DET_AMT=objDefault.getDefault("ARE_DET_AMT",_Entity.ARE_DET_AMT);
						_Entity.ARE_RMKS=objDefault.getDefault("ARE_RMKS",_Entity.ARE_RMKS);
						_Entity.ARE_DR_AMT=objDefault.getDefault("ARE_DR_AMT",_Entity.ARE_DR_AMT);
						_Entity.ARE_CR_AMT=objDefault.getDefault("ARE_CR_AMT",_Entity.ARE_CR_AMT);
						_Entity.DIFF_DET_AMT=objDefault.getDefault("DIFF_DET_AMT",_Entity.DIFF_DET_AMT);

					// WriteDefault CCYRTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"CCYRTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					CCYRTBMEntity objDCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objDCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objDCCYRTBMEntity.CCY_CODE;
						 _Entity.XCHG_RATE=objDCCYRTBMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

					// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;
						 _Entity.SIGN_CODE=objDACCDTBMEntity.SIGN_CODE;

					}else{
					}
			}else{
			}
			}

    decimal cTOTAL_ARE_DR_AMT=0;
    decimal cTOTAL_ARE_CR_AMT=0;

    QueryRows RsAREDTLM = new QueryRows(this.Trans);
    RsAREDTLM.CommandText = "SELECT * FROM AREDTLM WHERE ARE_NO='"+_Entity.ARE_NO+"'";
    RsAREDTLM.Open();
    while (!RsAREDTLM.EOF)
    {
        if (RsAREDTLM.getValue("SIGN_CODE")=="D")
        {
            cTOTAL_ARE_DR_AMT=cTOTAL_ARE_DR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }else{
            cTOTAL_ARE_CR_AMT=cTOTAL_ARE_CR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }
        RsAREDTLM.MoveNext();
    }
    RsAREDTLM.Close();

    _Entity.ARE_DR_AMT=cTOTAL_ARE_DR_AMT;
    _Entity.ARE_CR_AMT=cTOTAL_ARE_CR_AMT;

    _Entity.DIFF_DET_AMT=_Entity.ARE_DR_AMT-_Entity.ARE_CR_AMT;

				return _Entity;
		}

		public List<UID02206BE> Default(List<UID02206BE> _Entitys)
		{
			List<UID02206BE> _New_Entitys=new List<UID02206BE>();
			foreach (UID02206BE _Entity in _Entitys) 
			{
				_New_Entitys.Add(Default(_Entity));
			}
			return _New_Entitys;
		}

		public UID02206BE Search(VContexts _VContexts,UID02206BE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02206B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02206B","NotAccessRight", this.ZZLOGON_ID));
				return _Entity;
			}

			ZZSQLStatement  _SQLStatement = new ZZSQLStatement();

			_SQLStatement.Trans         = this.Trans;
			_SQLStatement.UID_CODE      = "UID02206B";
			_SQLStatement.ZZLOGON_SSD   = this.ZZLOGON_SSD;
			_SQLStatement.ZZLOGON_LANG  = this.ZZLOGON_LANG;
			_SQLStatement.DbName        = this.DbName;
			_SQLStatement.Process("QID02206B");

			ZZQLite oZZQLite = _SQLStatement.oZZQLite;

			if (_VContexts.getValue("T1")!="" && _VContexts.getValue("V1")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T1"), _VContexts.getValue("V1"));
			}
			if (_VContexts.getValue("T2")!="" && _VContexts.getValue("V2")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T2"), _VContexts.getValue("V2"));
			}
			if (_VContexts.getValue("T3")!="" && _VContexts.getValue("V3")!="")
			{
				oZZQLite.AddXWhereClause("", _VContexts.getValue("T3"), _VContexts.getValue("V3"));
			}

			if (!Util.IsNullOrEmpty(_VContexts.getValue("lookup")))
			{
				oZZQLite.AddXWhereClause("A", "", _VContexts.getValue("lookup"));
			}

						_VContexts.setValue("TT1","AREDTLM.ARE_NO");
						_VContexts.setValue("VV1",_Entity.ARE_NO);
						oZZQLite.AddWhereClause("AREDTLM", "ARE_NO", _Entity.ARE_NO);

			oZZQLite.Condition=_VContexts.getValue("Condition");
			oZZQLite.Make();

			int _AbsolutePage = _VContexts.getInteger("AbsolutePage");
			int _PageSize     = _VContexts.getInteger("ZZPageSize");
			int _Restrict     = _VContexts.getInteger("Restrict");
			int _TotalPages   = 0;

			_Setting.Trans      = this.Trans;

			if (_Restrict == 0)
			{
				_Restrict=30;
			}

			if (_PageSize <= 0)
			{
				_PageSize=_Setting.getInteger("ZZPageSize","20");
			}

				QueryRows RsAREDTLM   = new QueryRows(this.Trans);
			RsAREDTLM.CommandText = oZZQLite.SQLStatement;
			RsAREDTLM.Top         = _Restrict;
			RsAREDTLM.Skip        = (_AbsolutePage-1)*_PageSize;
			RsAREDTLM.Limit       = _PageSize;
			RsAREDTLM.CountAll    = true;
			RsAREDTLM.Open();

			if (_PageSize <= 0)
			{
				_TotalPages   = RsAREDTLM.RecordCount;
				_PageSize = RsAREDTLM.RecordCount;
			}
			else
			{
				_TotalPages= RsAREDTLM.RecordCount / _PageSize;
			}
			if (RsAREDTLM.RecordCount > (RsAREDTLM.RecordCount / _PageSize) * _PageSize)
			{
				_TotalPages = _TotalPages+ 1;
			}

			if (_AbsolutePage <= 0)
			{
				_AbsolutePage = 1;
			}

			if (_AbsolutePage > _TotalPages)
			{
				_AbsolutePage = _TotalPages;
			}

			Wxf _Wxf  = new Wxf();

			_Wxf.XDeclareStart();
			foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
			{
				if (_AttributeMapping.STATUS_CODE != "HIDDEN")
				{
					_Wxf.XDeclare(_AttributeMapping);
				}
			}
			_Wxf.XDeclareFinal();

			_Wxf.Variable("ZZPageSize",_PageSize);
			_Wxf.Variable("ZZPages",_TotalPages);
			_Wxf.Variable("USQL_CODE","QID02206B");

			while (!RsAREDTLM.EOF)
			{
				string cPickUpData   = "";
				object cValue;

				_Wxf.AddNew();
				int i=0;
				foreach(AttributeMapping _AttributeMapping in oZZQLite.AttributeMappings)
				{

					cValue = RsAREDTLM[i];

					if (_AttributeMapping.STATUS_CODE != "HIDDEN")
					{
						_Wxf[_AttributeMapping.Name] = cValue;
					}

					if (_AttributeMapping.PrimaryKey)
					{
						if (cPickUpData != "")
						{
							cPickUpData = cPickUpData + "|";
						}
						cPickUpData = cPickUpData + ZZGeneral.Base64Encoder(_AttributeMapping.ColumnName + "=" + cValue);
					}
					i++;
				}

				_Wxf.PK_DATA   = cPickUpData;
				_Wxf.Update();

				RsAREDTLM.MoveNext();
			}
			RsAREDTLM.Close();

			_Entity.Wxf=_Wxf;

			return _Entity;
		}

		public UID02206BE Retrieves(VContexts _VContexts,UID02206BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02206B", this.ZZLOGON_ID, "ACCESS")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02206B","NotAccessRight",this.ZZLOGON_ID));
				return _Entity;
			}

			List<UID02206BE> _New_Entitys=new List<UID02206BE>();

				CriteriaRetrieve objRetrieveAREDTLMEntity=new CriteriaRetrieve(typeof(AREDTLMEntity));

			objRetrieveAREDTLMEntity.Top=1;
			objCondition=objRetrieveAREDTLMEntity.NewCondition();

			bool ZZOrderByAsc=_Setting.getBoolean("ZZOrderByAsc","YES");
			switch (_VContexts.getValue("UDEF_CODE"))
			{
				case "MF":
					{
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);
						break;
					}
				case "ML":
					{
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",!ZZOrderByAsc);
						break;
					}
				case "MN":
					{
						if (ZZOrderByAsc)
						{
							objCondition.GreaterThan("ARE_DT",_Entity.ARE_DT);
						}else{
							objCondition.LessThan("ARE_DT",_Entity.ARE_DT);
						}
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);
						break;
					}
				case "MP":
					{
						if (ZZOrderByAsc)
						{
							objCondition.LessThan("ARE_DT",_Entity.ARE_DT);
						}else{
							objCondition.GreaterThan("ARE_DT",_Entity.ARE_DT);
						}
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",!ZZOrderByAsc);
						break;
					}
				case "CANCEL":
					{
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);
						_Entity.DATA_STATUS_CODE="Verified";
						break;
					}
				case "LOOKUP":
					{
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);
						break;
					}

				case "ACTIVETAB":
					{
								if (_Entity.ARE_DT != "")
								{
									objCondition.Equals("ARE_DT", _Entity.ARE_DT);
								}

						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);
						break;
					}

				case "READ":
					{
								objCondition.Equals("ARE_DT", _Entity.ARE_DT);
						break;
					}					
				default:
					{
								if (_Entity.ARE_DT != "")
								{
									objCondition.Equals("ARE_DT", _Entity.ARE_DT);
								}
						objRetrieveAREDTLMEntity.OrderBy("ARE_DT",ZZOrderByAsc);

						break;
					}
			}

			//filter by quick look
			string _LOOUP_COLUMN_NAME=";AREDTLM.ARE_NO;AREDTLM.ARE_DT;AREDTLM.CCY_CODE;AREDTLM.ACC_CODE;AREDTLM.SIGN_CODE;AREDTLM.DEPT_CODE;AREDTLM.ARE_ANAY_CODE01;AREDTLM.ARE_ANAY_CODE02;AREDTLM.ARE_ANAY_CODE03;AREDTLM.ARE_ANAY_CODE04;AREDTLM.ARE_ANAY_CODE05;AREDTLM.ARE_ANAY_CODE06;AREDTLM.ARE_RMKS";
			string lookup=_VContexts.getValue("lookup");
			_LOOUP_COLUMN_NAME=_LOOUP_COLUMN_NAME+";";
			if (lookup!="")
			{
				Condition _Condition2=objCondition.NewCondition(" AND ");
				foreach (string s in _LOOUP_COLUMN_NAME.Split(new char[] { ';' }))
				{
					if (!string.IsNullOrEmpty(s))
					{
						Condition OrCondition=_Condition2.NewCondition();
						OrCondition.Expression(s,lookup);
					}
				}
			}

			//filter by filters

				//filter by AccessConditions
				_DataPermission.AccessCondition(objCondition);

						objCondition.Equals("ARE_NO", _Entity.ARE_NO);

			AREDTLMEntity objAREDTLMs=(AREDTLMEntity)this.Trans.RetrieveEntity(objRetrieveAREDTLMEntity);

			ZZTrace.Debug(ZFILE_NAME,"SELECT="+objRetrieveAREDTLMEntity.SELECTClause);

			if (objAREDTLMs==null && _VContexts.getValue("UDEF_CODE")=="MN")
			{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataEOF"));
				return _Entity;
			}else if (objAREDTLMs==null && _VContexts.getValue("UDEF_CODE")=="MP")
			{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataBOF"));
				return _Entity;
			}else if (objAREDTLMs==null && (_VContexts.getValue("UDEF_CODE")=="MF" || _VContexts.getValue("UDEF_CODE")=="ML") )
			{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataBlank"));
				return _Entity;
			}else if (objAREDTLMs==null && _VContexts.getValue("UDEF_CODE")=="LOOKUP")
			{
				/*
				 *if (this.COLUMN_REFX.IndexOf(";")>=0)
				 *{
				 *}else{
				 *    string _T_COLUMN_REFX=this.COLUMN_REFX;
				 *    if (_T_COLUMN_REFX.IndexOf('.') > 0)
				 *    {
				 *        string[] s = _T_COLUMN_REFX.Split(new char[] { '.' });
				 *        _T_COLUMN_REFX=s[1];
				 *    }
				 *    _Entity.Exceptions.Add(new LogisticException(_T_COLUMN_REFX,"DataNotFd","DataBlank"));
				 *    return _Entity;
				 *}
				 */
			}else if (objAREDTLMs==null && _VContexts.getValue("UDEF_CODE")=="ACTIVETAB")
			{
				//throw new LogisticException("ARE_DT","Activetab","");
				//this.Default(_Entity);
			}else if (objAREDTLMs==null)
			{
				//this.Default(_Entity);
			}else{
				AREDTLMEntity objAREDTLMEntity = objAREDTLMs;
					ZZTrace.MUKI_DEBUG_LOG=true;
						_Entity.ARE_NO=objAREDTLMEntity.ARE_NO;
						_Entity.ARE_DT=objAREDTLMEntity.ARE_DT;
						_Entity.SERIAL_NO=objAREDTLMEntity.SERIAL_NO;
						_Entity.CCY_CODE=objAREDTLMEntity.CCY_CODE;
						_Entity.XCHG_RATE=objAREDTLMEntity.XCHG_RATE;
						_Entity.ACC_CODE=objAREDTLMEntity.ACC_CODE;
						_Entity.SIGN_CODE=objAREDTLMEntity.SIGN_CODE;
						_Entity.ARE_CCY_AMT=objAREDTLMEntity.ARE_CCY_AMT;
						_Entity.ARE_DET_AMT=objAREDTLMEntity.ARE_DET_AMT;
						_Entity.ARE_RMKS=objAREDTLMEntity.ARE_RMKS;

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objAREDTLMEntity.ZADD_DATE);
_Entity.UZADD_USER     =objAREDTLMEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objAREDTLMEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objAREDTLMEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objAREDTLMEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objAREDTLMEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objAREDTLMEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objAREDTLMEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objAREDTLMEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objAREDTLMEntity.ZSECURITY_CODE6;

					// WriteDefault CCYRTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"CCYRTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					CCYRTBMEntity objDCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objDCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objDCCYRTBMEntity.CCY_CODE;
						 _Entity.XCHG_RATE=objDCCYRTBMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

					// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

    decimal cTOTAL_ARE_DR_AMT=0;
    decimal cTOTAL_ARE_CR_AMT=0;

    QueryRows RsAREDTLM = new QueryRows(this.Trans);
    RsAREDTLM.CommandText = "SELECT * FROM AREDTLM WHERE ARE_NO='"+_Entity.ARE_NO+"'";
    RsAREDTLM.Open();
    while (!RsAREDTLM.EOF)
    {
        if (RsAREDTLM.getValue("SIGN_CODE")=="D")
        {
            cTOTAL_ARE_DR_AMT=cTOTAL_ARE_DR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }else{
            cTOTAL_ARE_CR_AMT=cTOTAL_ARE_CR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }
        RsAREDTLM.MoveNext();
    }
    RsAREDTLM.Close();

    _Entity.ARE_DR_AMT=cTOTAL_ARE_DR_AMT;
    _Entity.ARE_CR_AMT=cTOTAL_ARE_CR_AMT;

    _Entity.DIFF_DET_AMT=_Entity.ARE_DR_AMT-_Entity.ARE_CR_AMT;

					_Entity.DATA_STATUS_CODE="Verified";
			}
				return _Entity;
		}

		public  UID02206BE AddNew(UID02206BE _Entity)
		{

			ZZTrace.Debug(ZFILE_NAME,"ZZLOGON_ID="+this.ZZLOGON_ID);

			if (_DocsPermission.AccessRight("UID02206B", this.ZZLOGON_ID, "ADDNEW")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02206B","NotAddNewRight",this.ZZLOGON_ID));
				return _Entity;
			}

			_Entity.ATVT_CODE="ADDNEW";

				_Entity=this.Verify(_Entity);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}
			AREDTLMEntity objAddNewEntity=new AREDTLMEntity();

			ZZHDKeyLib objZZHDKeyLib=new ZZHDKeyLib();
			objZZHDKeyLib.Trans=this.Trans;
							objZZHDKeyLib.LNK_COLUMN("ARE_NO",_Entity.ARE_NO);
							objZZHDKeyLib.LNK_COLUMN_VALUE=_Entity.ARE_NO;

				objZZHDKeyLib.PREFIX_CODE="UID02206B";
				objZZHDKeyLib.getSerial("AREDTLM");
				int XSERIAL_NO=objZZHDKeyLib.PREFIX_CN;

				_Entity.ARE_DT=objZZHDKeyLib.DT_NO;

				ZZTrace.Debug(ZFILE_NAME,_Entity.ARE_DT);
				ZZTrace.Debug(ZFILE_NAME,XSERIAL_NO);

					objAddNewEntity.ARE_DT=_Entity.ARE_DT;

			this.Trans.RetrieveEntity(objAddNewEntity);

			if (objAddNewEntity.Verified)
			{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataExist",_Entity.ARE_DT));
				return _Entity;
			}else{

				objAddNewEntity.ZADD_DATE = DateTime.Now;
				objAddNewEntity.ZADD_USER = this.ZZLOGON_ID;

				_DataPermission.TABLE_NAME="AREDTLM";
				_DataPermission.DataAccessRight();
				objAddNewEntity.ZSECURITY_CODE1 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE1");
				objAddNewEntity.ZSECURITY_CODE2 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE2");
				objAddNewEntity.ZSECURITY_CODE3 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE3");
				objAddNewEntity.ZSECURITY_CODE4 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE4");
				objAddNewEntity.ZSECURITY_CODE5 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE5");
				objAddNewEntity.ZSECURITY_CODE6 = _DataPermission.ACCESS_AUTH.getValue("SECURITY_CODE6");
				objAddNewEntity.ZSECURITY_RIGHT = _DataPermission.ACCESS_AUTH.getInteger("SECURITY_RIGHT");

				_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZADD_DATE);
_Entity.UZADD_USER     =objAddNewEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objAddNewEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objAddNewEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objAddNewEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objAddNewEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objAddNewEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objAddNewEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objAddNewEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objAddNewEntity.ZSECURITY_CODE6;

    _Entity.ARE_DET_AMT=_Entity.XCHG_RATE*_Entity.ARE_CCY_AMT;

							objAddNewEntity.ARE_NO=_Entity.ARE_NO;
							objAddNewEntity.ARE_DT=_Entity.ARE_DT;
							objAddNewEntity.SERIAL_NO=_Entity.SERIAL_NO;
							objAddNewEntity.CCY_CODE=_Entity.CCY_CODE;
							objAddNewEntity.XCHG_RATE=_Entity.XCHG_RATE;
							objAddNewEntity.ACC_CODE=_Entity.ACC_CODE;
							objAddNewEntity.SIGN_CODE=_Entity.SIGN_CODE;
							objAddNewEntity.ARE_CCY_AMT=_Entity.ARE_CCY_AMT;
							objAddNewEntity.ARE_DET_AMT=_Entity.ARE_DET_AMT;
							objAddNewEntity.ARE_RMKS=_Entity.ARE_RMKS;
					ZZTrace.Debug(ZFILE_NAME,XSERIAL_NO);

					objAddNewEntity.SERIAL_NO=XSERIAL_NO;
					_Entity.SERIAL_NO=XSERIAL_NO;
			}

				this.Trans.SaveChanges(objAddNewEntity);

				// WriteDefault CCYRTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"CCYRTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					CCYRTBMEntity objDCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objDCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objDCCYRTBMEntity.CCY_CODE;
						 _Entity.XCHG_RATE=objDCCYRTBMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

				// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

    decimal cTOTAL_ARE_DR_AMT=0;
    decimal cTOTAL_ARE_CR_AMT=0;

    QueryRows RsAREDTLM = new QueryRows(this.Trans);
    RsAREDTLM.CommandText = "SELECT * FROM AREDTLM WHERE ARE_NO='"+_Entity.ARE_NO+"'";
    RsAREDTLM.Open();
    while (!RsAREDTLM.EOF)
    {
        if (RsAREDTLM.getValue("SIGN_CODE")=="D")
        {
            cTOTAL_ARE_DR_AMT=cTOTAL_ARE_DR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }else{
            cTOTAL_ARE_CR_AMT=cTOTAL_ARE_CR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }
        RsAREDTLM.MoveNext();
    }
    RsAREDTLM.Close();

    _Entity.ARE_DR_AMT=cTOTAL_ARE_DR_AMT;
    _Entity.ARE_CR_AMT=cTOTAL_ARE_CR_AMT;

    _Entity.DIFF_DET_AMT=_Entity.ARE_DR_AMT-_Entity.ARE_CR_AMT;

				_Entity.DATA_STATUS_CODE="Verified";
			return _Entity;
		}

		public  UID02206BE Update(UID02206BE _Entity)
		{

			if (_DocsPermission.AccessRight("UID02206B", this.ZZLOGON_ID, "UPDATE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02206B","NotUpdateRight",this.ZZLOGON_ID));
				return _Entity;
			}
			_Entity.ATVT_CODE="UPDATE";

						if (_Entity.ARE_DT=="")
						{
							_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataMiss",""));
						}

			_Entity=this.Verify(_Entity);
			ZZTrace.Debug(ZFILE_NAME,"_Entity.Exceptions.Count="+_Entity.Exceptions.Count);
			if (_Entity.Exceptions.Count>0)
			{
				return _Entity;
			}

					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ARE_NO="+_Entity.ARE_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ARE_DT="+_Entity.ARE_DT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.SERIAL_NO="+_Entity.SERIAL_NO);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.XCHG_RATE="+_Entity.XCHG_RATE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.SIGN_CODE="+_Entity.SIGN_CODE);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ARE_CCY_AMT="+_Entity.ARE_CCY_AMT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ARE_DET_AMT="+_Entity.ARE_DET_AMT);
					//ZZTrace.Debug(ZFILE_NAME,"_Entity.ARE_RMKS="+_Entity.ARE_RMKS);

			objRetrieveEntity=new CriteriaRetrieve(typeof(AREDTLMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("ARE_DT", _Entity.ARE_DT);

			_DataPermission.UpdateCondition(objCondition);

			EntityObject objEntityObject=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objEntityObject != null)
			{
				AREDTLMEntity objUpdateEntity=(AREDTLMEntity)objEntityObject;
				if (objUpdateEntity.Verified)
				{
					objUpdateEntity.ZCHG_DATE=DateTime.Now;
					objUpdateEntity.ZCHG_USER=this.ZZLOGON_ID;

    _Entity.ARE_DET_AMT=_Entity.XCHG_RATE*_Entity.ARE_CCY_AMT;

								objUpdateEntity.ARE_NO=_Entity.ARE_NO;
								objUpdateEntity.ARE_DT=_Entity.ARE_DT;
								objUpdateEntity.SERIAL_NO=_Entity.SERIAL_NO;
								objUpdateEntity.CCY_CODE=_Entity.CCY_CODE;
								objUpdateEntity.XCHG_RATE=_Entity.XCHG_RATE;
								objUpdateEntity.ACC_CODE=_Entity.ACC_CODE;
								objUpdateEntity.SIGN_CODE=_Entity.SIGN_CODE;
								objUpdateEntity.ARE_CCY_AMT=_Entity.ARE_CCY_AMT;
								objUpdateEntity.ARE_DET_AMT=_Entity.ARE_DET_AMT;
								objUpdateEntity.ARE_RMKS=_Entity.ARE_RMKS;

						this.Trans.SaveChanges(objUpdateEntity);

						// WriteDefault CCYRTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.CCY_CODE="+_Entity.CCY_CODE);
			if (!Util.IsNullOrEmpty(_Entity.CCY_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(CCYRTBMEntity));//货币资料表
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("CCY_CODE",_Entity.CCY_CODE);
				_DataPermission.TABLE_NAME="CCYRTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"CCYRTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					CCYRTBMEntity objDCCYRTBMEntity=(CCYRTBMEntity)objEntityContainer[0];
					if (objDCCYRTBMEntity.Verified)
					{
						 _Entity.CCY_CODE=objDCCYRTBMEntity.CCY_CODE;
						 _Entity.XCHG_RATE=objDCCYRTBMEntity.XCHG_RATE;

					}else{
					}
			}else{
			}
			}

						// WriteDefault ACCDTBM
ZZTrace.Debug(ZFILE_NAME,"_Entity.ACC_CODE="+_Entity.ACC_CODE);
			if (!Util.IsNullOrEmpty(_Entity.ACC_CODE))
			{
			objRetrieveEntity=new CriteriaRetrieve(typeof(ACCDTBMEntity));//Chart Of Account
			objRetrieveEntity.Top=1;

			objCondition=objRetrieveEntity.NewCondition();
			objCondition.Equals("ACC_CODE",_Entity.ACC_CODE);
				_DataPermission.TABLE_NAME="ACCDTBM";
				_DataPermission.AccessCondition(objCondition);
			objEntityContainer=this.Trans.RetrieveEntitys(objRetrieveEntity);

ZZTrace.Debug(ZFILE_NAME,"ACCDTBM"+objEntityContainer.Count);
			if (objEntityContainer.Count>0)
			{

					ACCDTBMEntity objDACCDTBMEntity=(ACCDTBMEntity)objEntityContainer[0];
					if (objDACCDTBMEntity.Verified)
					{
						 _Entity.ACC_CODE=objDACCDTBMEntity.ACC_CODE;
						 _Entity.ACC_NAME01=objDACCDTBMEntity.ACC_NAME01;

					}else{
					}
			}else{
			}
			}

    decimal cTOTAL_ARE_DR_AMT=0;
    decimal cTOTAL_ARE_CR_AMT=0;

    QueryRows RsAREDTLM = new QueryRows(this.Trans);
    RsAREDTLM.CommandText = "SELECT * FROM AREDTLM WHERE ARE_NO='"+_Entity.ARE_NO+"'";
    RsAREDTLM.Open();
    while (!RsAREDTLM.EOF)
    {
        if (RsAREDTLM.getValue("SIGN_CODE")=="D")
        {
            cTOTAL_ARE_DR_AMT=cTOTAL_ARE_DR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }else{
            cTOTAL_ARE_CR_AMT=cTOTAL_ARE_CR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }
        RsAREDTLM.MoveNext();
    }
    RsAREDTLM.Close();

    _Entity.ARE_DR_AMT=cTOTAL_ARE_DR_AMT;
    _Entity.ARE_CR_AMT=cTOTAL_ARE_CR_AMT;

    _Entity.DIFF_DET_AMT=_Entity.ARE_DR_AMT-_Entity.ARE_CR_AMT;

						_Entity.UZADD_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZADD_DATE);
_Entity.UZADD_USER     =objUpdateEntity.ZADD_USER;
_Entity.UZCHG_DATE     =_Convert.ToDateTimeString(objUpdateEntity.ZCHG_DATE);
_Entity.UZCHG_USER     =objUpdateEntity.ZCHG_USER;
_Entity.USECURITY_CODE1=objUpdateEntity.ZSECURITY_CODE1;
_Entity.USECURITY_CODE2=objUpdateEntity.ZSECURITY_CODE2;
_Entity.USECURITY_CODE3=objUpdateEntity.ZSECURITY_CODE3;
_Entity.USECURITY_CODE4=objUpdateEntity.ZSECURITY_CODE4;
_Entity.USECURITY_CODE5=objUpdateEntity.ZSECURITY_CODE5;
_Entity.USECURITY_CODE6=objUpdateEntity.ZSECURITY_CODE6;

					_Entity.DATA_STATUS_CODE="Verified";

				}else{
					_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataNotFd"));
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataNotFd"));
			}
			return _Entity;
		}

		public  UID02206BE Delete(UID02206BE _Entity)
		{
			if (_DocsPermission.AccessRight("UID02206B", this.ZZLOGON_ID, "DELETE")==false)
			{
				_Entity.Exceptions.Add(new LogisticException("UID02206B","NotDeleteRight"));
				return _Entity;
			}
			_Entity.ATVT_CODE="DELETE";

					if (_Entity.ARE_DT=="")
					{
						_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataMiss"));
						return _Entity;
					}

    CriteriaRetrieve objValidateEntity=new CriteriaRetrieve(typeof(AREHDTMEntity));
    objCondition=objValidateEntity.NewCondition();
    objCondition.Equals("ARE_NO",_Entity.ARE_NO);

    EntityObject objValidateObject=this.Trans.RetrieveEntity(objValidateEntity);

    if(objValidateObject!=null)
    {
        AREHDTMEntity objAREHDTMEntity=(AREHDTMEntity)objValidateObject;

        ZZStatusCheck oZZStatusCheck=new ZZStatusCheck();
        oZZStatusCheck.Trans=this.Trans;
        if (!oZZStatusCheck.Validate("ODTP_CODE_DT",objAREHDTMEntity.ODTP_STATUS,_Entity.ATVT_CODE))
        {
            throw new LogisticException("ODTP_STATUS",oZZStatusCheck.Caption,objAREHDTMEntity.ODTP_STATUS);
        }
    }

				objRetrieveEntity=new CriteriaRetrieve(typeof(AREDTLMEntity));
			objCondition=objRetrieveEntity.NewCondition();

					objCondition.Equals("ARE_DT", _Entity.ARE_DT);

			_DataPermission.DeleteCondition(objCondition);

			EntityObject objDeleteEntity=this.Trans.RetrieveEntity(objRetrieveEntity);

			if (objDeleteEntity != null)
			{
				if (objDeleteEntity.Verified)
				{
						this.Trans.DeleteEntity(objDeleteEntity);

    decimal cTOTAL_ARE_DR_AMT=0;
    decimal cTOTAL_ARE_CR_AMT=0;

    QueryRows RsAREDTLM = new QueryRows(this.Trans);
    RsAREDTLM.CommandText = "SELECT * FROM AREDTLM WHERE ARE_NO='"+_Entity.ARE_NO+"'";
    RsAREDTLM.Open();
    while (!RsAREDTLM.EOF)
    {
        if (RsAREDTLM.getValue("SIGN_CODE")=="D")
        {
            cTOTAL_ARE_DR_AMT=cTOTAL_ARE_DR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }else{
            cTOTAL_ARE_CR_AMT=cTOTAL_ARE_CR_AMT+Math.Abs(RsAREDTLM.getDecimal("ARE_DET_AMT"));
        }
        RsAREDTLM.MoveNext();
    }
    RsAREDTLM.Close();

    _Entity.ARE_DR_AMT=cTOTAL_ARE_DR_AMT;
    _Entity.ARE_CR_AMT=cTOTAL_ARE_CR_AMT;

    _Entity.DIFF_DET_AMT=_Entity.ARE_DR_AMT-_Entity.ARE_CR_AMT;

						StringBuilder delete_cmd=new StringBuilder(this.ZZLOGON_ID+"UID02206B-DELETED=");
							delete_cmd.Append("ARE_DT="+_Entity.ARE_DT);

					ZZTrace.Debug("DELETED_LOG",delete_cmd.ToString());

				}else{
					_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataNotFd"));
					return _Entity;
				}
			}else{
				_Entity.Exceptions.Add(new LogisticException("ARE_DT","DataNotFd"));
			}
			return _Entity;

		}

		public UID02206BE UserDefine(VContexts cMethod,UID02206BE _Entity)
		{
				return _Entity;
		}

#region Field
			private CriteriaRetrieve objRetrieveEntity;
		private List<EntityObject>  objEntityContainer;
		private Condition        objCondition;
		private ZZDefault        objDefault      = new ZZDefault();
		private ZZDataPermission _DataPermission = new ZZDataPermission();
		private ZZDocsPermission _DocsPermission = new ZZDocsPermission();
		private ZZSetting        _Setting        = new ZZSetting();
		private XConvert         _Convert        = new XConvert();

#endregion

			const  string ZFILE_NAME   ="UID02206BM";
		const  string MUKI_EDITION ="MIN";

	}
}
